#include "keyboardInfo.h"
#include "editErrorCostHolders.h"

double keyboardErrorsCostHolder::insertionCost( char symbBeforeInsertion, char insertion ) const
{
	std::string consonants = "qrtpsdfgklzcbnm";
	if( consonants.find(symbBeforeInsertion) != std::string::npos && symbBeforeInsertion == insertion ) {
		return 1 - penaltyRelief;
	}
	return 1.0;
}

double keyboardErrorsCostHolder::substitutionCost( char source, char target ) const
{
	if( source == target ) {
		return 0.0;
	} 
	if( keyboardInfo::isKeyboardNeighbor( source, target ) ) {
		return 1.0 - penaltyRelief;
	}
	return 1.0; 
}

double phoneticErrorsCostHolder::insertionCost( char symbBeforeInsertion, char insertion ) const
{
  std::string consonants = "qrtpsdfgklzcbnm";
  if( consonants.find(symbBeforeInsertion) != std::string::npos && symbBeforeInsertion == insertion ) {
    return 0.3;
  }
  const int COMBINATIONS_COUNT = 2;
  std::string combinations[COMBINATIONS_COUNT] = {
    "cq", "ck", 
  };
  for (int i = 0; i < COMBINATIONS_COUNT; ++i) {
    if (combinations[i][0] == symbBeforeInsertion && combinations[i][1] == insertion) {
      return 0.3;
    }
  }
  return 1.0;
}

double phoneticErrorsCostHolder::substitutionCost( char source, char target ) const
{
  if( source == target ) {
    return 0.0;
  } 
  if ((source == 's' && target == 'c') || (source == 'c' && target == 's')) {
    return 0.5;
  }
  std::string vowels = "aeiuo";
  if (vowels.find(source) != std::string::npos && vowels.find(target) != std::string::npos) {
    return 0.8;
  }
  return 1.0; 
}

double phoneticErrorsCostHolder::transpositionCost( char source, char target ) const
{
  if (source == target) {
    return 0.0;
  }
  const int COMBINATIONS_COUNT = 1;
  std::string combinations[COMBINATIONS_COUNT] = {
    "ei"
  };
  for (int i = 0; i < COMBINATIONS_COUNT; ++i) {
    if ((source == combinations[i][0] && target == combinations[i][1]) ||
        (source == combinations[i][1] && target == combinations[i][0]))
    {
      return 0.3;
    }
  }
  return 0.5;
}

double phoneticErrorsCostHolder::deletionCost( char symbAfterDeletion, char deletion ) const
{
  std::string consonants = "qrtpsdfgklzcbnm";
  if( consonants.find( symbAfterDeletion ) != std::string::npos && symbAfterDeletion == deletion ) {
      return 0.3;
  }

  return 1.0;
}

// added deletion costs
double keyboardErrorsCostHolder::deletionCost( char symbAfterDeletion, char deletion ) const
{
	std::string consonants = "qrtpsdfgklzcbnm";
	if( consonants.find( symbAfterDeletion ) != std::string::npos && symbAfterDeletion == deletion ) {
		return 1 - penaltyRelief;
	}

	// TODO - place for combinations

	return 1.0; 
}
